/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.landery.modules.sinco.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.landery.annotation.AnonymousAccess;
import me.landery.annotation.Log;
import me.landery.modules.sinco.domain.Cert;
import me.landery.modules.sinco.domain.Customize;
import me.landery.modules.sinco.service.CertService;
import me.landery.modules.sinco.service.dto.CertDto;
import me.landery.modules.sinco.service.dto.CertQueryCriteria;
import me.landery.modules.sinco.service.dto.CustomizeQueryCriteria;
import me.landery.modules.system.service.UserService;
import me.landery.modules.system.service.dto.UserDto;
import me.landery.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://el-admin.vip
* @author cc.w
* @date 2022-06-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "认证信息管理")
@RequestMapping("/sinco/cert")
public class CertController {

    private final CertService certService;
    private final UserService userService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('cert:list','admin')")
    public void exportCustomize(HttpServletResponse response, CertQueryCriteria criteria) throws IOException {
        certService.download(certService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询认证信息")
    @ApiOperation("查询认证信息")
    @PreAuthorize("@el.check('cert:list','admin')")
    public ResponseEntity<Object> queryCustomize(CertQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(certService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/status")
    @Log("查询当前用户认证信息")
    @ApiOperation("查询当前用户认证信息")
    @PreAuthorize("@el.check('cert:status','admin')")
    public ResponseEntity<Object> queryOwnStatus(){
        CertDto certDto = certService.findByUserId(SecurityUtils.getCurrentUserId());
        if (certDto != null){
            return new ResponseEntity<>(certDto,HttpStatus.OK);
        }else{
            return new ResponseEntity<>("no cert",HttpStatus.OK);
        }

    }

    @PostMapping
    @Log("新增认证信息")
    @ApiOperation("新增认证信息")
    @PreAuthorize("@el.check('cert:add','admin')")
    public ResponseEntity<Object> createCustomize(@Validated @RequestBody Cert resources){
        return new ResponseEntity<>(certService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改认证信息")
    @ApiOperation("修改认证信息")
    @PreAuthorize("@el.check('cert:edit','admin')")
    public ResponseEntity<Object> updateCustomize(@Validated @RequestBody Cert resources){
        certService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除认证信息")
    @ApiOperation("删除认证信息")
    @PreAuthorize("@el.check('cert:del','admin')")
    public ResponseEntity<Object> deleteCustomize(@RequestBody Long[] ids) {
        certService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}